package nix.hibernate;

import java.util.ArrayList;
import java.util.List;

import nix.utils.HibernateUtils;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

public class DemoCriteria {

	/**
	 * Criteria
	 */
	@Test
	public void fun1() {
		Configuration conf = new Configuration().configure();
		SessionFactory sessionFactory = conf.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction ts = session.beginTransaction();
		// 使用Criteria查询
		Criteria criteria = session.createCriteria(User.class);
		// 查询条件
		criteria.add(Restrictions.eq("name", "zhangSan"));// ==
		criteria.add(Restrictions.gt("age", 18));// >
		criteria.add(Restrictions.ge("age", 18));// >=
		criteria.add(Restrictions.lt("age", 18));// <
		criteria.add(Restrictions.le("age", 18));// <=
		criteria.add(Restrictions.ne("name", "zhangsan"));// !=
		criteria.add(Restrictions.in("age", new ArrayList()));// in
		criteria.add(Restrictions.between("age", 10, 20));// between
		criteria.add(Restrictions.like("name", "%zhang%"));// like
		criteria.add(Restrictions.isNotNull("name"));// is not null
		criteria.add(Restrictions.or(Restrictions.eq("age", "18"),
				Restrictions.eq("age", "19")));// or
		criteria.add(Restrictions.and(Restrictions.eq("age", "18"),
				Restrictions.eq("age", "19")));// and

		List<User> list = criteria.list();
		User user = (User) criteria.uniqueResult();
		ts.commit();
		// 关闭资源
		session.close();
		sessionFactory.close();
	}
	
	@Test
	public void fun2() {
		Session session = HibernateUtils.getCurrentSession();
		Transaction ts = session.beginTransaction();
		// 使用Criteria查询
		Criteria criteria = session.createCriteria(User.class);
		// 分页
		criteria.setFirstResult(0);
		criteria.setMaxResults(2);
		List<User> list = criteria.list();
		System.out.println(list);
		ts.commit();
	}
	
	@Test
	public void fun3() {
		Session session = HibernateUtils.getCurrentSession();
		Transaction ts = session.beginTransaction();
		// 使用Criteria查询
		Criteria criteria = session.createCriteria(User.class);
		// 总条数
		criteria.setProjection(Projections.rowCount());
		// criteria.setProjection(Projections.avg("id"));
		// criteria.setProjection(Projections.max("id"));
		Number num = (Number) criteria.uniqueResult();
		System.out.println(num.longValue());
		ts.commit();
	}
	
	@Test
	public void fun4() {
		Session session = HibernateUtils.getSession();
		Transaction ts = session.beginTransaction();
		// 使用Criteria查询
		Criteria criteria = session.createCriteria(User.class);
		// 排序
		criteria.addOrder(Order.desc("id"));
		List list = criteria.list();
		System.out.println(list);
		
		ts.commit();
		session.close();
	}
}
